package com.dadie.dorm.controller;



import com.dadie.dorm.pojo.House;
import com.dadie.dorm.pojo.query.HouseQuery;
import com.dadie.dorm.service.IHouseService;
import com.dadie.dorm.service.impl.HouseServiceImpl;
import com.dadie.dorm.util.JSONUtil;
import com.dadie.dorm.util.LayUITableResult;
import com.dadie.dorm.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/house")
public class HouseServlet extends HttpServlet {
    private IHouseService houseService = new HouseServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectByPage";
        }
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "deleteAll":
                deleteAll(req, resp);
                break;
            case "add":
                add(req, resp);
                break;

            case "update":
                update(req, resp);
                break;
            case "getHouseUpdatePage":
                getHouseUpdatePage(req,resp);
                break;
            default:
        }
    }

    private void getHouseUpdatePage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        House house=houseService.selectById(Integer.parseInt(id));
        req.setAttribute("house",house);
        req.getRequestDispatcher("house_update.jsp").forward(req,resp);
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("HouseServlet.selectByPage");
        String pageStr = req.getParameter("page");
        if (pageStr == null || pageStr.equals("")) {
            pageStr = "1";
        }
        String limitStr = req.getParameter("limit");
        if (limitStr == null || limitStr.equals("")) {
            limitStr = "10";
        }
        int page = Integer.parseInt(pageStr);
        int limit = Integer.parseInt(limitStr);
        String name = req.getParameter("name");

        HouseQuery houseQuery = new HouseQuery(page, limit, name);

        LayUITableResult layUITableResult = houseService.selectByPage(houseQuery);
        System.out.println("LayUITableResult: " + layUITableResult);

        JSONUtil.toJSON(resp, layUITableResult);
    }


    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("HouseServlet.deleteById");
        String id = req.getParameter("id");
        boolean isSuccess = houseService.deleteById(Integer.parseInt(id));
        //resp.sendRedirect(req.getContextPath() + "/house");

        Result result = isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
        JSONUtil.toJSON(resp, result);

    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        System.out.println("HouseServlet.deleteAll");
        //String id = req.getParameter("id");
        String[] ids = req.getParameterValues("ids[]");
        boolean isSuccess = houseService.deleteAll(ids);

        Result result = isSuccess ? Result.ok("删除成功") : Result.error("删除失败");
        JSONUtil.toJSON(resp, result);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        System.out.println("HouseServlet.add");
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");

        House house = new House(name,  phone);
        boolean isSuccess = houseService.add(house);
        //resp.sendRedirect(req.getContextPath() + "/house");

        Result result = isSuccess ? Result.ok("添加成功") : Result.error("添加失败");
        JSONUtil.toJSON(resp, result);


    }


    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("HouseServlet.update");
        String id = req.getParameter("id");
        System.out.println(id);
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");

        House house=new House(Integer.parseInt(id),name,phone);

        boolean isSuccess=houseService.update(house);
        Result result=isSuccess ? Result.ok("修改成功"):Result.error("修改失败");
        JSONUtil.toJSON(resp,result);
    }





}

